package com.p7.notepad.adapter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.p7.notepad.R;
import com.p7.notepad.bean.Notepad;
import com.p7.notepad.bean.NotepadInfo;
import com.tools.s7.recycler.BaseRecyclerAdapter;
import com.tools.s7.recycler.holder.BaseViewHolder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RecycleAdapter extends BaseRecyclerAdapter<Notepad.DataBean> {

    @Override
    protected int getLayoutId() {
        return R.layout.recycle_item;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int i, Notepad.DataBean dataBean) {
        holder.setOnClickListener(R.id.recycle_btn, mClick);
        holder.setOnClickListener(R.id.recycle_del, mClick);
        holder.setText(R.id.recycle_name, dataBean.getTitle());
        holder.setText(R.id.recycle_time, getTime(dataBean.getUpdateTime()));
        if (!TextUtils.isEmpty(dataBean.getInfo())) {
            List<NotepadInfo> data = new Gson().fromJson(dataBean.getInfo(), new TypeToken<List<NotepadInfo>>() {}.getType());
            for (NotepadInfo info : data) {
                if (!TextUtils.isEmpty(info.getInput())) {
                    holder.setText(R.id.recycle_info, info.getInput());
                    return;
                }
            }
        }
    }

    private String getTime(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = dateFormat.format(new Date(time));
        return date;

    }
}
